package ua.com.fesvlast.hotel.model.utils;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import ua.com.fesvlast.hotel.controller.managers.ConfigurationManager;

public class MailSender {
	private static final String from = ConfigurationManager.getInstance().getProperty(ConfigurationManager.HOST_EMAIL);
	private static  final String username = ConfigurationManager.getInstance().getProperty(ConfigurationManager.EMAIL_USERNAME);
    private static final String password = ConfigurationManager.getInstance().getProperty(ConfigurationManager.EMAIL_PASSWORD);
    private static final String host = ConfigurationManager.getInstance().getProperty(ConfigurationManager.POST_HOST);
    
    
    public static boolean sendMessage(String to, String msgSubject, String msgContent, String name){
    	
    	boolean flag=true;
    
    	Properties props = new Properties();
	      props.put("mail.smtp.auth", "true");
	      props.put("mail.smtp.starttls.enable", "true");
	      props.put("mail.smtp.host", host);
	      props.put("mail.smtp.port", "587");
	      
	      Session session = Session.getInstance(props, new javax.mail.Authenticator() {
	 	     
		         protected PasswordAuthentication getPasswordAuthentication() {
		            return new PasswordAuthentication(username, password);
		         }
		      });
		      
		      try {
		          // Create a default MimeMessage object.
		          Message message = new MimeMessage(session);

		          // Set From: header field of the header.
		          message.setFrom(new InternetAddress(from));

		          // Set To: header field of the header.
		          message.setRecipients(Message.RecipientType.TO,
		          InternetAddress.parse(to));

		          // Set Subject: header field
		          message.setSubject(msgSubject);

		          // Now set the actual message
		          message.setText(msgContent+"\n"+name);

		          // Send message
		          Transport.send(message);

		       } catch (MessagingException e) {
		    	   flag=false;
		           throw new RuntimeException(e);  
		       }
    	return flag;
    }


}
